clear
capture restore
set scheme cleanplots, perm

* SET DIRECTORIES
global home "..."

global data "$home/Data/"
global output "$home/Output/"

cd $home

*SET CONTROL VARIABLES
local firm_controls "log_salesg_real_1lag_std size_assets_1lag_std liquidity_1lag_std pcmq_1lag_std recpay2yq_1lag_std d2aq_1lag_std ln_market_cap_q_1lag_std tobins_q_1lag_std lev_d2c_1lag_std i.sector i.fqtr dd_1lag_std age_1lag_std"

local agg_controls "lngdp inflation unemp nasdaq fedfunds"

*************
***Options***
*************
*Set ivol measure to use as baseline
local matur m1545
*Set mp shock to use as baseline
local mps ff4_d
*Set redeploy variable
local r redeploy

* Set the LP details ========================================================================
*# periods to compute the responses foreach
local hor = 20
*# lags of "endogenous" variables 
local lagy = 4 
*# lags of "shock"
local lage = 0 

********************************************************************************
********************************************************************************
********************************************************************************
*Read-in ivol data (contains all trading days for Compustat firms)
use "${data}Compustat_Quarterly_Final", clear

gen fyear = year

merge m:1 gvkey fyear using "${data}redeploy_data"
drop if _merge==2
drop _merge

*Set panel data
xtset firmid ts_qtr

gen log_salesg_real_std = F.log_salesg_real_1lag_std

*Flag sample for those in at least 40 quarters
bysort gvkey: egen obs_num = count(gvkey)

local n = 20

*Flag outliers (bottom/top 0.5%) for investment
_pctile investment_intensive, p(.5 99.5)
gen 	investment_outlier = (investment_intensive<r(r1)|investment_intensive>r(r2))

*gen sample_period=(pre_crisis==1|post_crisis==1|crisis_flg==1)
*gen sample_period=(pre_crisis==1|post_crisis==1)
*gen sample_period=(post_crisis==1)
gen sample_period=(pre_crisis==1)
gen in_sample = (obs_num>=`n' & !missing(obs_num) & investment_outlier==0 & sample_period)

********************************************************************************
*Set panel data
xtset firmid ts_qtr

gen ivol_`matur'_std = ivol_`matur'
gen `r'_lag_std = `r'_lag
gen dd_std = dd
gen age_1lag_std = age

rename age age_1lag

***Standardize quarterly Compustat variables and lagged ivol level
foreach i in dd_1lag age_1lag log_salesg_real_1lag size_assets_1lag liquidity_1lag pcmq_1lag recpay2yq_1lag d2aq_1lag ln_market_cap_q_1lag tobins_q_1lag lev_d2c_1lag ivol_`matur' `r'_lag {

	*Pre-Crisis Standardization
	drop `i'_std
	sum `i' if in_sample
	gen `i'_std = (`i' - r(mean)) / r(sd)
	
}

*Standardize ivol and dd
_pctile ivol_`matur' if in_sample, percentiles(30 70)
gen ivol_`matur'_hi2lo = (ivol_`matur' - r(r1)) / (r(r2) -  r(r1))

_pctile `r'_lag  if in_sample, percentiles(30 70)
gen `r'_hi2lo = (`r'_lag - r(r1)) / (r(r2) -  r(r1))

*Standardize mps to unit standard deviation & positive is expansionary
egen date_tag=tag(ts_qtr)

sum log_salesg_real_std if pre_crisis
gen sales_gwth = log_salesg_real_std/r(sd)
	
gen `mps'=`mps'_shock_qtr

sum `mps' if pre_crisis & date_tag
replace `mps' = `mps'/r(sd)

********************************************************************************
*Prepare local projection variables
********************************************************************************
xtset firmid ts_qtr

*Use the inflation-adjusted versions of dependent variables, if applicable
foreach i in niq ivltq ivstq invtq intanq tanq xrdq saleq K_int K_int_Know K_int_Org K_int_offBS K_int_onBS {
	
	drop `i'
	rename `i'_real `i'
 
}


foreach var in capital_stock K_int_qtr K_int_onBS_qtr K_int_offBS_qtr K_int_Know_qtr K_int_Org_qtr K_tot_qtr {
	forvalues h =   0/`hor' {
		 gen `var'`h' = (ln(f`h'.`var') - ln(l.`var')) * 100
}
}

*Create time variable for DK SEs
xtset
egen time=group(ts_qtr)
xtset firmid time

drop if !in_sample

putexcel set results_`r'_temp_`mps'.xlsx, sheet(Lo2Hi) replace
local i=2

	foreach var in capital_stock K_int_Org_qtr { 
		
	eststo clear
		
	cap gen qtrs = _n-1 if _n<=`hor'+1
	cap gen zero =  0 	if _n<=`hor'+1
	cap gen b_hiUhiDD = 0
	cap gen u_hiUhiDD = 0
	cap gen d_hiUhiDD = 0

	cap gen b_hiUloDD = 0
	cap gen u_hiUloDD = 0
	cap gen d_hiUloDD = 0

	cap gen b_loUhiDD = 0
	cap gen u_loUhiDD = 0
	cap gen d_loUhiDD = 0

	cap gen b_loUloDD = 0
	cap gen u_loUloDD = 0
	cap gen d_loUloDD = 0
	
	cap gen b_mpsDD = 0
	cap gen u_mpsDD = 0
	cap gen d_mpsDD = 0
	cap gen uu_mpsDD = 0
	cap gen dd_mpsDD = 0
	
	cap gen b_mpsI = 0
	cap gen u_mpsI = 0
	cap gen d_mpsI = 0
	cap gen uu_mpsI = 0
	cap gen dd_mpsI = 0
	
	cap gen b_mpsDDI = 0
	cap gen u_mpsDDI = 0
	cap gen d_mpsDDI = 0
	cap gen uu_mpsDDI = 0
	cap gen dd_mpsDDI = 0
	
	cap gen nobs = 0

	********************************************************************************
	*INVESTMENT REGRESSIONS
	********************************************************************************

	*1. Investment on mps (plus interaction with ivol level)
	forvalues h = 0/`hor' {

		quietly: reghdfe `var'`h' `mps' L.ivol_`matur'_hi2lo L.`r'_hi2lo c.`mps'#c.L.ivol_`matur'_hi2lo c.`mps'#c.L.`r'_hi2lo c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo `firm_controls' l(1/`lagy').(`agg_controls') if in_sample, a(firmid) vce(cluster time,dkraay(16)) old
		
		replace b_loUloDD = _b[`mps']                     	if _n == `h'+1
		replace u_loUloDD = _b[`mps'] + 1.645* _se[`mps']  	if _n == `h'+1
		replace d_loUloDD = _b[`mps'] - 1.645* _se[`mps']  	if _n == `h'+1	
		
		quietly: lincom _b[`mps'] + _b[c.`mps'#c.L.`r'_hi2lo]
		replace b_loUhiDD = r(estimate) 									if _n == `h'+1
		replace u_loUhiDD = r(estimate) + 1.645* r(se)                   	if _n == `h'+1
		replace d_loUhiDD = r(estimate) - 1.645* r(se)                   	if _n == `h'+1
		
		quietly: lincom _b[`mps'] + _b[c.`mps'#c.L.ivol_`matur'_hi2lo]
		replace b_hiUloDD = r(estimate) 									if _n == `h'+1
		replace u_hiUloDD = r(estimate) + 1.645* r(se)                   	if _n == `h'+1
		replace d_hiUloDD = r(estimate) - 1.645* r(se)                   	if _n == `h'+1
		
		quietly: lincom _b[`mps'] + _b[c.`mps'#c.L.`r'_hi2lo] + _b[c.`mps'#c.L.ivol_`matur'_hi2lo] + _b[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo]
		replace b_hiUhiDD = r(estimate) 									if _n == `h'+1
		replace u_hiUhiDD = r(estimate) + 1.645* r(se)                   	if _n == `h'+1
		replace d_hiUhiDD = r(estimate) - 1.645* r(se)                   	if _n == `h'+1
		
		quietly: replace b_mpsDD = _b[c.`mps'#c.L.`r'_hi2lo]                     			if _n == `h'+1
		replace u_mpsDD = _b[c.`mps'#c.L.`r'_hi2lo] + 1.645* _se[c.`mps'#c.L.`r'_hi2lo]  	if _n == `h'+1
		replace d_mpsDD = _b[c.`mps'#c.L.`r'_hi2lo] - 1.645* _se[c.`mps'#c.L.`r'_hi2lo]  	if _n == `h'+1	
		replace uu_mpsDD = _b[c.`mps'#c.L.`r'_hi2lo] + _se[c.`mps'#c.L.`r'_hi2lo]  	if _n == `h'+1
		replace dd_mpsDD = _b[c.`mps'#c.L.`r'_hi2lo] - _se[c.`mps'#c.L.`r'_hi2lo]  	if _n == `h'+1	
		
		quietly: replace b_mpsI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo]                     			if _n == `h'+1
		replace u_mpsI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo] + 1.645* _se[c.`mps'#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1
		replace d_mpsI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo] - 1.645* _se[c.`mps'#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1	
		replace uu_mpsI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo] + _se[c.`mps'#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1
		replace dd_mpsI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo] - _se[c.`mps'#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1	
		
		quietly: replace b_mpsDDI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo]                     			if _n == `h'+1
		replace u_mpsDDI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo] + 1.645* _se[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo]  	if _n == `h'+1
		replace d_mpsDDI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo] - 1.645* _se[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo]  	if _n == `h'+1	
		replace uu_mpsDDI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo] + _se[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo]  	if _n == `h'+1
		replace dd_mpsDDI = _b[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo] - _se[c.`mps'#c.L.ivol_`matur'_hi2lo#c.L.`r'_hi2lo]  	if _n == `h'+1	
		
		replace nobs = e(N)                        						if _n == `h'+1	
		di e(N) 
		eststo
		
	}
		
	twoway(rarea u_mpsDDI d_mpsDDI qtrs, fcolor(gs14) lcolor(gs14) lw(none) lpattern(solid) xlabel(,labsize(medlarge)) ylabel(,labsize(medlarge))) ///
	(rarea uu_mpsDDI dd_mpsDDI qtrs, fcolor(gs8) lcolor(gs8) lw(none) lpattern(solid)) ///
	(line b_mpsDDI qtrs, lcolor(black) lpattern(solid) lwidth(thick)) /// 
	(line zero qtrs, lcolor(black)) if !missing(qtrs), legend(off) ///
	ytitle("Percent", size(medlarge)) xtitle("Quarter", size(medlarge)) ///
	graphregion(color(white)) plotregion(color(white))

	graph export "${output}figure4_`var'.png", as(png) width(510) height(390) replace
	putexcel Z`i' = picture("${output}figure4_`var'.png")


	drop qtrs zero b_hiUhiDD-nobs
	
	local i=`i'+20

	eststo clear	
}
